package com.travel.server.manager.impl.command.user;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.user.QueryUserListRequest;
import com.travel.server.manager.dto.response.user.QueryUserListResponse;
import com.travel.server.manager.dto.response.user.QueryUserListResponse.UserItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentDal;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.impl.dal.UserDal;
import com.travel.server.manager.impl.dal.UserDal.UserParam;
import com.travel.server.manager.model.DepartmentInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 12:45
 * @Desc:
 */
@Component
@Slf4j
public class QueryUserListCommand extends AbstractCommand<QueryUserListRequest, QueryUserListResponse> {

    @Autowired
    private UserDal userDal;

    @Autowired
    private DepartmentDal departmentDal;

    @Override
    protected void checkInput(QueryUserListRequest request) {
    }

    @Override
    public QueryUserListResponse handle(QueryUserListRequest request) {
        UserParam param = makeParam(request);

        PageDto<UserInfo> pageDto = userDal.select(param);
        return makeResponse(pageDto);
    }

    private QueryUserListResponse makeResponse(PageDto<UserInfo> pageDto){
        QueryUserListResponse response = new QueryUserListResponse();
        response.setTotalNum(pageDto.getTotal());
        List<UserInfo> list = pageDto.getList();
        List<UserItem> userList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(UserInfo userInfo : list){
                UserItem item = new UserItem();
                item.setId(userInfo.getId());
                item.setAccount(userInfo.getAccount());
                item.setCreateTime(userInfo.getCreateTime());
                DepartmentInfo departmentInfo = departmentDal.selectById(userInfo.getDepartmentId());
                if(departmentInfo != null) {
                    item.setDepartmentName(departmentInfo.getDepartmentName());
                    item.setDepartmentId(departmentInfo.getId());
                }
                item.setLastLoginTime(userInfo.getLastLoginTime());
                item.setMobile(userInfo.getMobile());
                item.setName(userInfo.getName());
                item.setState(userInfo.getEnable());
                item.setHeadUrl(userInfo.getHeadUrl());
                userList.add(item);
            }
        }
        response.setUserList(userList);
        return response;
    }

    private UserParam makeParam(QueryUserListRequest request){
        UserParam param = new UserParam();
        param.setLimit(request.getLimit());
        param.setOffset(request.getOffset());
        return param;
    }
}
